package com.zc.employee.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zc.employee.domain.Department;
import com.zc.employee.domain.Employee;
import com.zc.employee.domain.PageBean;
import com.zc.employee.service.DepartmentService;
import com.zc.employee.service.EmployeeService;

/**
 * 员工管理action类
 * @author 请叫我小纯洁丶
 *
 */
public class EmployeeAction extends ActionSupport implements ModelDriven<Employee>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 模型驱动
	 */
	private Employee employee = new Employee();
	@Override
	public Employee getModel() {
		// TODO Auto-generated method stub
		return employee;
	}
     //注入业务层
	private EmployeeService employeeService;
	private DepartmentService departmentService;

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	
	
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	private Integer currPage = 1;
	
	
	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}



	/**
	 * 登录方法
	 */
	public String login(){
		System.out.println("login执行了");
		//调用业务层类
		Employee existEmployee = employeeService.login(employee);
		
		if(existEmployee == null){
			//失败
			this.addActionError("用户名或密码错误");
			return INPUT;
		}else{
			//成功
			ActionContext.getContext().getSession().put("existEmployee", existEmployee);
			return SUCCESS;
		}

	}
	
	

	//分页查员工
	public String findAll(){
		
		PageBean<Employee> pageBean = employeeService.findByPage(currPage);
	
		//将pageBean存入值栈中
				ActionContext.getContext().getValueStack().push(pageBean);
		return "findAll";
	}
	
	//跳转到添加页面
	public String saveUI(){
		List<Department> list = departmentService.findAll();
		ActionContext.getContext().getValueStack().set("list", list);
		return "saveUI";
	}
	
    //执行添加方法
	public String save(){
		
		employeeService.save(employee);
		
		return "saveSuccess";
	}
	
	//编辑
	public String edit(){
		//根据员工编号查询
		employee = employeeService.findById(employee.getEid());
		//部门
		List<Department> list = departmentService.findAll();
		ActionContext.getContext().getValueStack().set("list", list);
		return "editSuccess";
	}
	//更新
	public String update(){
		employeeService.update(employee);
		return "updateSuccess";
	}
	//删除
	public String delete(){
		
		employee = employeeService.findById(employee.getEid());
		
		employeeService.delete(employee);
		return "deleteSuccess";
	}
}
